﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;


namespace CRM.Web.VehicleManage
{
    public partial class CustomerSearch : Common.PageBase
    {
        UserLoginInfo _userLoginInfo;
        public string ModifyUrl = "Modify.aspx?CUSTOMERID={0}";
        T_CUSTOMER_BLL cBll = new T_CUSTOMER_BLL();
        V_CUSTOMERVEHICLE_BLL bll = new V_CUSTOMERVEHICLE_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            UserLoginInfo.CheckLoginInfo();
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定




















































        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }



        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]))
            {
                sql.Append(" and CUSTOMERID like '%" + Request.QueryString["CUSTOMERID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERCODE"]))
            {
                sql.Append(" and CUSTOMERCODE like '%" + Request.QueryString["CUSTOMERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERNAME"]))
            {
                sql.Append(" and CUSTOMERNAME like '%" + Request.QueryString["CUSTOMERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTCONTACTTEL"]))
            {
                sql.Append(" and (MOBILEPHONE = '" + Request.QueryString["FIRSTCONTACTTEL"] + "' or HOMEPHONE = '" + Request.QueryString["FIRSTCONTACTTEL"] + "' or  FIRSTCONTACTTEL = '" + Request.QueryString["FIRSTCONTACTTEL"] + "' ) ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTCONTACTTYPE"]))
            {
                sql.Append(" and FIRSTCONTACTTYPE like '%" + Request.QueryString["FIRSTCONTACTTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MOBILEPHONE"]))
            {
                sql.Append(" and MOBILEPHONE like '%" + Request.QueryString["MOBILEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["HOMEPHONE"]))
            {
                sql.Append(" and HOMEPHONE like '%" + Request.QueryString["HOMEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ADDRESS"]))
            {
                sql.Append(" and ADDRESS like '%" + Request.QueryString["ADDRESS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COUNTRY"]))
            {
                sql.Append(" and COUNTRY like '%" + Request.QueryString["COUNTRY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PROVINCE"]))
            {
                sql.Append(" and PROVINCE like '%" + Request.QueryString["PROVINCE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PREFECTURECITY"]))
            {
                sql.Append(" and PREFECTURECITY like '%" + Request.QueryString["PREFECTURECITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CITY"]))
            {
                sql.Append(" and CITY like '%" + Request.QueryString["CITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VILLAGETOWN"]))
            {
                sql.Append(" and VILLAGETOWN like '%" + Request.QueryString["VILLAGETOWN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VILLAGE"]))
            {
                sql.Append(" and VILLAGE like '%" + Request.QueryString["VILLAGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GROUPTEAM"]))
            {
                sql.Append(" and GROUPTEAM like '%" + Request.QueryString["GROUPTEAM"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERBASE"]))
            {
                sql.Append(" and CUSTOMERBASE like '%" + Request.QueryString["CUSTOMERBASE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERTYPE"]))
            {
                sql.Append(" and CUSTOMERTYPE like '%" + Request.QueryString["CUSTOMERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERLEVEL"]))
            {
                sql.Append(" and CUSTOMERLEVEL like '%" + Request.QueryString["CUSTOMERLEVEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SPECIALTYPE"]))
            {
                sql.Append(" and SPECIALTYPE like '%" + Request.QueryString["SPECIALTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIGCUSTOMERTYPE"]))
            {
                sql.Append(" and BIGCUSTOMERTYPE like '%" + Request.QueryString["BIGCUSTOMERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SPECIALLIST"]))
            {
                sql.Append(" and SPECIALLIST like '%" + Request.QueryString["SPECIALLIST"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["IDNUMBER"]))
            {
                sql.Append(" and IDNUMBER like '%" + Request.QueryString["IDNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["URL"]))
            {
                sql.Append(" and URL like '%" + Request.QueryString["URL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMAIL"]))
            {
                sql.Append(" and EMAIL like '%" + Request.QueryString["EMAIL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ZIPCODE"]))
            {
                sql.Append(" and ZIPCODE like '%" + Request.QueryString["ZIPCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FAX"]))
            {
                sql.Append(" and FAX like '%" + Request.QueryString["FAX"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID like '%" + Request.QueryString["CREATEPEOPLEID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID like '%" + Request.QueryString["LASTMODIFYPEOPLEID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUSLOG"]))
            {
                sql.Append(" and STATUSLOG like '%" + Request.QueryString["STATUSLOG"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FTIMESTAMPBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("FTIMESTAMP", Request.QueryString["FTIMESTAMPBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FTIMESTAMPEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("FTIMESTAMP", Request.QueryString["FTIMESTAMPEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["AGE"]))
            {
                sql.Append(" and AGE like '%" + Request.QueryString["AGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISACCEPTSMS"]))
            {
                sql.Append(" and ISACCEPTSMS like '%" + Request.QueryString["ISACCEPTSMS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISACCEPTINFORMATION"]))
            {
                sql.Append(" and ISACCEPTINFORMATION like '%" + Request.QueryString["ISACCEPTINFORMATION"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("BIRTHDATE", Request.QueryString["BIRTHDATEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("BIRTHDATE", Request.QueryString["BIRTHDATEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERID"]))
            {
                sql.Append(" and DEALERID like '%" + Request.QueryString["DEALERID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERNAME"]))
            {
                sql.Append(" and DEALERNAME like '%" + Request.QueryString["DEALERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERCODE"]))
            {
                sql.Append(" and DEALERCODE like '%" + Request.QueryString["DEALERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ORGNAME"]))
            {
                sql.Append(" and ORGNAME like '%" + Request.QueryString["ORGNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ZONENAME"]))
            {
                sql.Append(" and ZONENAME like '%" + Request.QueryString["ZONENAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID like '%" + Request.QueryString["BUSINESSDIVISIONID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '%" + Request.QueryString["BUSINESSDIVISIONCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BECOMEDATEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("BECOMEDATE", Request.QueryString["BECOMEDATEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BECOMEDATEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("BECOMEDATE", Request.QueryString["BECOMEDATEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURCHASENUMBER"]))
            {
                sql.Append(" and PURCHASENUMBER like '%" + Request.QueryString["PURCHASENUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VINCODE"]))
            {
                sql.Append(" and VINCODE like '%" + Request.QueryString["VINCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINENUMBER"]))
            {
                sql.Append(" and ENGINENUMBER like '%" + Request.QueryString["ENGINENUMBER"] + "%'");
            }
            if (sql.Length == 0)
            {
                return sql;
            }
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }

            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {

            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion


            List<V_CUSTOMERVEHICLE_Entity> list;

            if (sql.Length == 0)
            {
                gvList.DataSource = new List<V_CUSTOMERVEHICLE_Entity>();
                gvList.DataKeyNames = new string[] { "CUSTOMERID" };
                gvList.DataBind();
                return;
            }
            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "CUSTOMERID" };
            gvList.DataBind();

        }

        /// <summary>
        /// 设置选中JS 命令函数
        /// Selecteds the command.
        /// </summary>
        /// <param name="customerCode">The customer code.</param>
        /// <param name="customerName">Name of the customer.</param>
        /// <param name="contactName">Name of the contact.</param>
        /// <param name="contactTel">The contact tel.</param>
        /// <param name="mobilePhone">The mobile phone.</param>
        /// <param name="homePhone">The home phone.</param>
        /// <param name="customerID">The customer ID.</param>
        /// <returns></returns>
        public string SelectedCommand(string customerCode, string customerName, string contactName, string contactTel,string mobilePhone, string homePhone, string customerID, string address)
        {
            string fun = "return setValue('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}'); ";
            fun = String.Format(fun, customerCode, customerName, contactName, contactTel, mobilePhone,  homePhone,  customerID, address);
            return fun;
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);

        }


        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_CUSTOMER_Entity entity = new T_CUSTOMER_Entity();
            entity.CUSTOMERID = (sender as GridView).DataKeys[e.RowIndex].Values[0].ToString();

            long rel = cBll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {

        }
        #endregion
		
    }
}
